package com.scott.prototype.greatestsage;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.*;
import java.util.Date;

/**
 * 齐天大圣
 */
@Setter
@Getter
@ToString
public class TheGreatestSage extends Monkey implements Cloneable,Serializable {

    private static final long serialVersionUID = 8613412976579524089L;
    /**
     * 有一个金箍棒
     */
    private GoldRingedStaff goldRingedStaff;

    /**
     * 从石头缝里面蹦出来
     */
    TheGreatestSage() {
        this.goldRingedStaff = new GoldRingedStaff();
        this.setBirthday(new Date());
        this.setHeight(150);
        this.setWeight(30);
    }

    /**
     * 分身术
     *
     * @return 自己
     */
    @Override
    protected Object clone() throws CloneNotSupportedException {
//            return super.clone();

        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;

        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;


        bos = new ByteArrayOutputStream();
        try {
            oos = new ObjectOutputStream(bos);
            oos.writeObject(this);

            bis = new ByteArrayInputStream(bos.toByteArray());
            ois = new ObjectInputStream(bis);

            TheGreatestSage copy = (TheGreatestSage) ois.readObject();
            copy.setBirthday(new Date());
            return copy;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return null;
    }

    void change() throws CloneNotSupportedException {
        TheGreatestSage copy = (TheGreatestSage) clone();

        System.out.println("本尊生日:" + this.getBirthday().getTime());
        System.out.println("变化出来的生日:" + copy.getBirthday().getTime());
        System.out.println("本尊和变化出来的是否是一个对象:" + (this == copy));
        System.out.println("本尊的金箍棒和变化出来的金箍棒是否是一个对象:" + (this.getGoldRingedStaff() == copy.getGoldRingedStaff()));


    }
}
